/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.com.jsepc.rep.report;

import cn.com.jsepc.rep.comm.Common;
import cn.com.jsepc.rep.comm.ConstUtil;
import cn.com.jsepc.rep.vo.FactTableVO;
import cn.com.jsepc.rep.vo.RepDataVO;
import com.jarp.db.DAO;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.internet.MailDateFormat;
import javax.servlet.ServletException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 *
 * @author 赵远
 * @time 2013-6-7
 */
public class AutoSetValue {

    public String setValue(String repid, String roleid, String demJsonArr, String repval, String sumval, String preval, String tpreval, String trepval, String tsumval,String starttime,String endtime) {
        String res = "";
        String factsql = this.getDataGrid(repid, demJsonArr);  //事实表
        Date date = new Date();
        DAO dao = new DAO();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(date);
        JSONArray json = new JSONArray();
        String dimCol = "";    //维度列名
        String dimValueCol = ""; //维度值
        String dimColWan = "";   //完成值
        String dimValueColWan = "";
        if (demJsonArr != null && !demJsonArr.equals("")) {
            JSONObject qryjson = JSONObject.fromString(demJsonArr);
            JSONArray demArr = qryjson.getJSONArray("demlist");
            for (int i = 0; i < demArr.length(); i++) {
                JSONObject jsonDem = demArr.getJSONObject(i);
                String facdimtable = jsonDem.getString("facdimtable_weidu");
                String facdimcodecol = jsonDem.getString("facdimcodecol_weidu");
                String facdimvalue = jsonDem.getString("facdimvalue_weidu");
                String facdimwanvalue = jsonDem.getString("facdimvaluewan");
                String getFact = "SELECT t.FACDIMCOL FROM TB_DEMENSION t WHERE t.DIMTABLE='" + facdimtable + "'";
                try {
                    String factCol = dao.getDataValue(ConstUtil.DBNAME, getFact);
                    dimCol += "DIM" + (i + 1) + "," + "DIMVALUE" + (i + 1) + ",";
                    dimValueCol += "'" + factCol + "'," + "'" + facdimvalue + "',";
                    dimColWan += "DIMWAN" + (i + 1) + "," + "DIMVALUEWAN" + (i + 1) + ",";
                    dimValueColWan += "'" + factCol + "'," + "'" + facdimwanvalue + "',";
                } catch (ServletException ex) {
                    Logger.getLogger(AutoSetValue.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } else {
            System.out.println("demJsonArr is null");
        }
        String sql = "INSERT INTO TB_SETVALUE(ID,REPID,REFACT,ROLEID,TIME," + dimCol + dimColWan + "REPVALUE,SUMVALUE,TREPVAL,TSUMVAL,PREVAL,TPREVAL,STARTTIME,ENDTIME,ADDTIME) values(" + new Common().getSeqStr("SEQ_SETVALUE") + "," + repid + ",'" + factsql + "','" + roleid + "','" + time + "'," + dimValueCol + dimValueColWan + "'" + repval + "','" + sumval 
                + "','"+trepval+"','"+tsumval+"','"+preval+"','"+tpreval+"','"+starttime+"','"+endtime+"'," + "NULL)";
        try {
            System.out.println("sql:"+sql);
            int count = dao.execCommand(ConstUtil.DBNAME, sql);
            if (count > 0) {
                res = "自动设定值任务启动成功！";
            } else {
                res = "自动设定值任务启动失败！";
            }
        } catch (ServletException ex) {
            Logger.getLogger(AutoSetValue.class.getName()).log(Level.SEVERE, null, ex);
        }
        return res;
    }

    public String getDataGrid(String repid, String demJsonArr) {
        String res = "";
        RepDataVO repVo = new FillDataDAO().getRepDemension(repid);
        if (repVo != null) {
            String tablename = repVo.getReffact(); // TF_REP_HRCNT
            res = tablename;
            FactTableVO.setSql(res);
        }
        return res;
    }
}
